<template>
  <div class="container" v-loading="isLoading">
    <el-card :key="isKey">
      <template #header>
        <el-form inline :model="searchForm" ref="searchForm">
          <el-form-item label="标题">
            <el-input
              v-model="searchForm.search"
              placeholder="请输入标题"
            ></el-input>
          </el-form-item>
          <el-form-item label="活动名称">
            <el-select v-model="searchForm.activity">
              <el-option label="全部" value="" />
              <el-option
                v-for="(oVal, oIdx) of activityList"
                :key="oIdx"
                v-bind="oVal"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="报名时间" prop="$createTime">
            <el-date-picker
              v-model="searchForm.$createTime"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitSearch">查询</el-button>
          </el-form-item>
        </el-form>
      </template>
      <el-table :data="tableData" border>
        <el-table-column prop="title" label="活动标题" />
        <el-table-column label="实名信息">
          <template #default="{row}">
            <RealName :name-id="row.realname_id"/>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" />
        <el-table-column prop="nickname" label="微信昵称" />
        <el-table-column prop="headimgurl" label="微信头像">
          <template #default="{ row }">
            <el-image
              style="width: 100px; height: 100px"
              :src="row.headimgurl"
              :preview-src-list="[row.headimgurl]"
            />
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="报名时间" />
      </el-table>
    </el-card>
    <footer class="footer">
      <el-pagination
        background
        layout="total, sizes, pager, jumper"
        :current-page="pagination.page"
        :page-sizes="[10, 20, 30, 50, 100]"
        :page-size="pagination.page_size"
        :total="tableTotal"
        @size-change="(val) => (pagination.page_size = val)"
        @current-change="(val) => (pagination.page = val)"
      />
    </footer>

    <el-dialog
      :title="builingForm.$id ? '编辑' : '新增'"
      :visible.sync="showAddOrEdit"
      width="666px"
      @closed="() => $refs['builingForm'].resetFields()"
    >
      <el-form
        :model="builingForm"
        :rules="builingRules"
        ref="builingForm"
        label-width="5em"
      >
        <el-form-item prop="$id" hidden />
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="builingForm.name"
            placeholder="请输入名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button
          type="primary"
          @click="submitRowData('builingForm')"
          :loading="loading.update"
          >确 定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { table, addOrEdit } from "@/utils/mixins";
import API from "@/api";
import RealName from '@/components/RealName.vue';
export default {
  mixins: [table, addOrEdit],
  components: {
    RealName
  },
  data: () => ({
    searchForm: {
      $action: ["getVoluntActivityEnlistList"],
      $createTime: [],
      search: "",
      activity: "",
    },
    builingForm: {
      name: "",
    },
    builingRules: {},
    activityList: [], // 活动列表
  }),
  computed: {
    activityObj() {
      return this.activityList.reduce(
        (acc, cur) => ((acc[cur.value] = cur.label), acc),
        {}
      );
    },
  },
  watch: {
    "searchForm.$createTime"(time) {
      let [create_time__gt, create_time__lt] = time || [];
      Object.assign(this.searchForm, { create_time__gt, create_time__lt });
    },
  },
  created() {
    API.getVoluntActivityList({ page_size: 100 })
      .then(({ data }) => {
        this.activityList = data.map((v) => ({
          label: v.title,
          value: v.id,
        }));
      })
      .catch();
  },
  methods: {},
};
</script>

<style lang="scss" scoped></style>
